/**
 * @file wexcube_port.c
 * @author JeremyWang (jeremywang0102@gmail.com / gin0101@126.com)
 * @brief WeXCube 接口文件
 * @version 
 * @date 2024-11-25
 * 
 * @copyright Copyright (c) 2024
 * 
 */

#include "wexcube_port.h"
#include "wexcube.h"

#include "driver/gpio.h"
#include "driver/uart.h"


/**
 * @brief 串口初始化函数
 * 
 */
void wex_port_init(void)
{
	// UART configuration
	const uart_config_t uart_config = {
		.baud_rate = 9600,
		.data_bits = UART_DATA_8_BITS,
		.parity = UART_PARITY_DISABLE,
		.stop_bits = UART_STOP_BITS_1,
		.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
	};

	// Configure UART parameters
	ESP_ERROR_CHECK(uart_param_config(UART_NUM_2, &uart_config));

	// Set UART pins
	ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, GPIO_NUM_11, GPIO_NUM_12, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));

	// Install UART driver
	ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, 256, 256, 0, NULL, 0));
}


/**
 * @brief 发送数据
 * 
 * @param pucData 数据指针
 * @param ucLen 数据长度
 */
void wex_port_send(wex_u8_t *pucData, wex_u8_t ucLen)
{
	// 通过自定义串口发送
	uart_write_bytes(UART_NUM_2, pucData, ucLen);

	// 通过调试串口发送
	printf("WEX_CMD[");
	for (wex_u8_t i = 0; i < ucLen; i++)
		printf("%c", (char)(*pucData++));
	printf("]\r\n");
}
